﻿using GrapeCity.Forguncy.Plugin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Vant.WpfControls.EditorSettings
{
    /// <summary>
    /// Interaction logic for LoadingTypeEditor.xaml
    /// </summary>
    public partial class LoadingTypeEditor : UserControl
    {
        public LoadingTypeEditor()
        {
            InitializeComponent();
            this.root.DataContext = new LoadingTypeEditorViewModel();
            this.ViewModel.PropertyChanged += ViewModel_PropertyChanged;
            this.DataContextChanged += LoadingTypeEditor_DataContextChanged;
        }

        private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if(e.PropertyName == nameof(LoadingTypeEditorViewModel.SelectedIndex))
            {
                var propertySettings = this.DataContext as IEditorSettingsDataContext;
                if (propertySettings != null)
                {
                    propertySettings.Value = this.ViewModel.SelectedIndex == 0 ? "spinner" : "circular";
                }
            }
        }

        LoadingTypeEditorViewModel ViewModel
        {
            get
            {
                return this.root.DataContext as LoadingTypeEditorViewModel;
            }
        }

        public class LoadingTypeEditorViewModel : ModelBase
        {
            private int _selectedIndex = 0;
            public int SelectedIndex
            {
                get
                {
                    return this._selectedIndex;
                }
                set
                {
                    if (_selectedIndex != value)
                    {
                        this._selectedIndex = value;
                        this.OnPropertyChanged();
                    }
                }
            }

        }

        private void LoadingTypeEditor_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var propertySettings = this.DataContext as IEditorSettingsDataContext;
            if (propertySettings != null)
            {
                if(object.Equals(propertySettings.Value, "spinner"))
                {
                    this.ViewModel.SelectedIndex = 0;
                }
                else
                {
                    this.ViewModel.SelectedIndex = 1;
                }
            }
        }
    }
}
